using System;
using System.Collections.Generic;
using TTEngine.TTMath;
using TTEngine.Graphics;

namespace TTEngine.Resource
{
    public static class DefSprite
    {

        public static byte[] Scale255(byte[] from)
        {
            byte[] to = new byte[from.Length];
            for (int i = 0; i < from.Length; i++)
            {
                to[i] = (byte)(from[i] * 255);
            }
            return to;
        }
        public static byte[] Scale128(byte[] from)
        {
            byte[] to = new byte[from.Length];
            for (int i = 0; i < from.Length; i++)
            {
                to[i] = (byte)(from[i] * 127);
            }
            return to;
        }

        public static void InitInnerResource(PackedTextureGroup packedelem)
        {
            // TODO: 初始化内置 shader
            // InitInnerShader();

            // WhiteSprite
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);

                byte[] baseData = new byte[8 * 8];
                for (int y = 0; y < spdata.Height; y++)
                {
                    for (int x = 0; x < spdata.Width; x++)
                    {
                        baseData[y * spdata.Width + x] = 1;
                    }
                }
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("white", ref elem);
            }

            // Border
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);
                byte[] baseData = new byte[]
                {
                    1, 1, 1, 1, 1, 1, 1, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 1, 1, 1, 1, 1, 1, 1,
                };
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("border", ref elem);
            }

            // Border2
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);
                byte[] baseData = new byte[]
                {
                    1, 1, 1, 1, 1, 1, 1, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 0, 1, 1, 1, 1, 0, 1,
                    1, 0, 1, 0, 0, 1, 0, 1,
                    1, 0, 1, 0, 0, 1, 0, 1,
                    1, 0, 1, 1, 1, 1, 0, 1,
                    1, 0, 0, 0, 0, 0, 0, 1,
                    1, 1, 1, 1, 1, 1, 1, 1,
                };
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("border2", ref elem);
            }

            // BorderRound
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);
                byte[] baseData = new byte[]
                {
                    0, 0, 0, 1, 1, 0, 0, 0,
                    0, 0, 1, 1, 1, 1, 1, 0,
                    0, 1, 1, 0, 0, 1, 1, 0,
                    1, 1, 0, 0, 0, 0, 1, 1,
                    1, 1, 0, 0, 0, 0, 1, 1,
                    0, 1, 1, 0, 0, 1, 1, 0,
                    0, 0, 1, 1, 1, 1, 1, 0,
                    0, 0, 0, 1, 1, 0, 0, 0,
                };
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("borderr", ref elem);
            }

            // Round
            {
                var spdata = new SpriteData(12, 12, ImageFormat.Gray, 6, 6);
                byte[] baseData = new byte[]
                {
                    0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
                    0, 0, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0,
                    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
                    0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0,
                    0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
                    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1,
                    0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0,
                    0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0,
                    0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
                    0, 0, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0,
                    0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
                };
                spdata.SetData(Scale128(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("round", ref elem);
            }

            // Arrow
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);
                byte[] baseData = new byte[]
                {
                    1, 1, 1, 1, 1, 0, 0, 0,
                    1, 0, 1, 1, 0, 0, 0, 0,
                    1, 1, 0, 1, 1, 0, 0, 0,
                    1, 1, 1, 0, 1, 1, 0, 0,
                    1, 0, 1, 1, 0, 1, 1, 0,
                    0, 0, 0, 1, 1, 0, 1, 1,
                    0, 0, 0, 0, 1, 1, 1, 0,
                    0, 0, 0, 0, 0, 1, 0, 0,
                };
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("arrow", ref elem);
            }

            // Corner
            {
                var spdata = new SpriteData(8, 8, ImageFormat.Gray);
                byte[] baseData = new byte[]
                {
                    0, 0, 0, 0, 0, 0, 0, 1,
                    0, 0, 0, 0, 0, 0, 1, 0,
                    0, 0, 0, 0, 0, 1, 0, 0,
                    0, 0, 0, 0, 1, 0, 0, 1,
                    0, 0, 0, 1, 0, 0, 1, 1,
                    0, 0, 1, 0, 0, 1, 0, 1,
                    0, 1, 0, 0, 1, 0, 0, 1,
                    1, 0, 0, 1, 1, 1, 1, 1,
                };
                spdata.SetData(Scale255(baseData));

                var elem = packedelem.AddSpriteInfo(spdata, SpriteEffect.GrayAsAlpha, Border.Zero);
                packedelem.AddElement("corner", ref elem);
            }



            packedelem.ApplyTextureData();
        }




    }
}
